import Vue from 'vue'
import VueRouter from 'vue-router'

// 路由要使用这个文件, 就需要导入
import Index from '../views/Index.vue'
import Goods from '../views/Goods.vue'
import User from '../views/User.vue'
import Detail from '../views/goods/Detail.vue'
import NoPay from '../views/user/NoPay.vue'
import Pay from '../views/user/Pay.vue'
import Login from '../views/user/Login.vue'


Vue.use(VueRouter)

const routes = [
	{
		path:'/',
		component:Index,
	},
	{
		path:'/goods',
		alias:'/g',
		component:Goods,
	},
	{
		path:'/login',
		component:Login,
	},
	{
		path:'/goods/detail', 
		// 动态路由,detail后面的参数会议id的形式传过来
		// path:'/goods/detail/:id',
		
		name:'detail',//声明路由, 给路由取一个名字,传参的时候就不用path了
		component:Detail,
	},
	{
		path:'/user',
		component:User,
		children:[
			{path:'pay',component:Pay},
			{path:'nopay',component:NoPay},
		]
	},
	{
		path:'*',
		redirect:'/'
	}
	 
]

const router = new VueRouter({
	//默认hash模式
	// mode:'history',
    routes
})
//全局路由守卫
router.beforeEach((to,from,next)=>{
	// 演示效果,点击商品/goods跳到到用户中心/user/pay,点击其他正常
	// 做登陆功能的话, 如果用户没有登录,跳到登陆界面,登录了的话,继续走下一步
	// 这是全局的,所有的路由都要进行这个验证
	// if(to.path=='/goods'){
	// 	next('/user/pay')
	// }else{
	// 	next()
	// }
		next()
})

export default router
